#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int Max = 0;
    int Depth(TreeNode* root)
    {
        if (root == nullptr)
        {
            return 0;
        }
        int leftD = Depth(root->left);
        int rightD = Depth(root->right);
        if (leftD + rightD > Max) Max = leftD + rightD;
        return max(leftD, rightD) + 1;
    }
    int diameterOfBinaryTree(TreeNode* root)
    {
        if (root == nullptr) return 0;
        if (root->left == nullptr && root->right == nullptr) return 0;
        int leftD = Depth(root->left);
        int rightD = Depth(root->right);
        if (leftD + rightD > Max) Max = leftD + rightD;
        return Max;
    }
};